//
//  YLRiskAssessmentBaseView.m
//  YLFinanceService
//
//  Created by she1323 on 2017/9/19.
//  Copyright © 2017年 she1323. All rights reserved.
//

#import "YLRiskAssessmentBaseView.h"
#import "YLGlobeHeader.h"
#import <Masonry.h>
#import "YLRiskAssessmentCell.h"
#import "YLRiskAssessmentGroup.h"
#import "WHProgressHUD.h"
@interface YLRiskAssessmentBaseView ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) NSMutableDictionary *heightAtIndexPath;//缓存高度

@property (nonatomic, weak) UILabel *titleLabel;

@property (nonatomic, weak) UITableView *tableView;

@property (nonatomic, weak) UIButton *nextButton;

@end

@implementation YLRiskAssessmentBaseView

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        [self setUpUI];
        
        self.backgroundColor = [UIColor whiteColor];
        
        self.layer.shadowOpacity = 0.2;// 阴影透明度
        
        self.layer.shadowColor = YLLOrgColor.CGColor;// 阴影的颜色
        
        self.layer.shadowRadius = 4;// 阴影扩散的范围控制
        
        
    }
    return self;
}

- (void)setUpUI
{
    // 底部
    __weak typeof(self) weakSelf = self;
    UIView *baseView = [[UIView alloc] init];
    [self addSubview:baseView];
    [baseView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(weakSelf);
        make.left.mas_equalTo(weakSelf).mas_offset(10);
        make.right.mas_equalTo(weakSelf).mas_equalTo(-10);
//        make.height.mas_equalTo(100);
    }];
    
    UILabel *titleLabel = [[UILabel alloc] init];
    _titleLabel = titleLabel;
    titleLabel.numberOfLines = 0;
    [baseView addSubview:titleLabel];
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.left.mas_equalTo(baseView);
        make.top.mas_equalTo(baseView).mas_offset(30);
        make.bottom.mas_equalTo(baseView).mas_offset(-30);
    }];
    
    titleLabel.text = @"";
    
    UIView *line = [[UIView alloc] init];
    [baseView addSubview:line];
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.mas_equalTo(baseView);
        make.height.mas_equalTo(1);
    }];
    
    line.backgroundColor = YLLineColor;
    line.alpha = 0.2;
    
    // 底部
    UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
    _tableView = tableView;
    tableView.backgroundColor = [UIColor whiteColor];
    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [tableView registerNib:[UINib nibWithNibName:NSStringFromClass([YLRiskAssessmentCell class]) bundle:nil] forCellReuseIdentifier:[YLRiskAssessmentCell cellID]];
    tableView.delegate = self;
    tableView.dataSource = self;
    [self addSubview:tableView];
    [tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(weakSelf);
        make.bottom.mas_equalTo(weakSelf).mas_offset(-44);
        make.top.mas_equalTo(baseView.mas_bottom);
    }];
    
    
    UIView *line1 = [[UIView alloc] init];
    [self addSubview:line1];
    line1.backgroundColor = YLLOrgColor;
    line1.alpha = 0.2;
    [line1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(weakSelf);
        make.top.mas_equalTo(tableView.mas_bottom);
        make.height.mas_equalTo(1);
    }];
    
    
    UIButton *nextButton = [UIButton buttonWithType:UIButtonTypeCustom];

  
    _nextButton = nextButton;
    [nextButton setTitle:@"下一题" forState:UIControlStateNormal];
    [nextButton setTitleColor:YLLOrgColor forState:UIControlStateNormal];
    [self addSubview:nextButton];
    [nextButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(line1.mas_bottom);
        make.bottom.right.mas_equalTo(weakSelf);
        make.left.mas_equalTo(weakSelf);
    }];
    
    [nextButton addTarget:self action:@selector(next) forControlEvents:UIControlEventTouchUpInside];
}

- (void)setGroup:(YLRiskAssessmentGroup *)group
{
    _group = group;
    _titleLabel.text = group.title;
    [self.tableView reloadData];
    
}

- (void)next
{
    
    NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
    if (indexPath == nil) {
        [WHProgressHUD showError:@"请选择一个答案"];
        return;
    }
    
    if ([_nextButton.currentTitle isEqualToString:@"提交"]) {
        
        if (self.delegate && [self.delegate respondsToSelector:@selector(riskAssessmentBaseView:titleNumber:answerNumber:)]) {
            [self.delegate riskAssessmentBaseView:self titleNumber:_group.titleNumber answerNumber:indexPath.row + 1];
        }
        
        if (self.delegate && [self.delegate respondsToSelector:@selector(riskAssessmentBaseViewSummitAnswer:)]) {
            [self.delegate riskAssessmentBaseViewSummitAnswer:self];
        }
        
    } else {
        
        if (self.delegate && [self.delegate respondsToSelector:@selector(riskAssessmentBaseView:titleNumber:answerNumber:)]) {
            [self.delegate riskAssessmentBaseView:self titleNumber:_group.titleNumber answerNumber:indexPath.row + 1];
        }
    }
   
 
    
    //[self.tableView reloadData];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.group.models.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 5;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    YLRiskAssessmentCell *cell = [tableView dequeueReusableCellWithIdentifier:[YLRiskAssessmentCell cellID] forIndexPath:indexPath];
    cell.model = self.group.models[indexPath.row];
    return cell;
}

- (void)setShowNext:(BOOL)showNext
{
    _showNext = showNext;
    if (showNext == YES) {
        [_nextButton setTitle:@"下一题" forState:UIControlStateNormal];
    } else {
        [_nextButton setTitle:@"提交" forState:UIControlStateNormal];
    }
}


#pragma mark - UITableViewDelegate
-(CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    // 先用缓存cell的高度(这样就可以点击状态栏回顶部了)
    // 解释一下，就是用一个字典做容器，在cell将要显示的时候在字典中保存这行cell的高度。然后在调用estimatedHeightForRowAtIndexPath方法时，先去字典查看有没有缓存高度，有就返回，没有就返回一个大概高度
    NSNumber *height = [self.heightAtIndexPath objectForKey:indexPath];
    if(height != nil)
    {
        return height.floatValue;
    }
    else
    {
        // 这个的意思就是告诉tableView，你需要自己适应高度，我不给你算啦哈哈哈。但是我们需要告诉它一个大概高度，例如上面的100，理论上这个是可以随便写的，并不影响显示结果，但是越接近真实高度越好
        // self.tableView.rowHeight = UITableViewAutomaticDimension;self.tableView.estimatedRowHeight = 100;
        return 100;
        // 如果我们用了自动计算高度的方法，又调用了tableView的reloadData方法（例如我们的数据有分页的时候，加载完下一页的数据后会去刷新tableView）。这时候就会出现问题，点击状态栏就有几率不能精确滚动到顶部了：
        // 解决这个问题的办法是去缓存cell的高度
    }
}



- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSNumber *height = @(cell.frame.size.height);
    [self.heightAtIndexPath setObject:height forKey:indexPath];
}

#pragma mark - Getters
- (NSMutableDictionary *)heightAtIndexPath
{
    if (!_heightAtIndexPath) {
        _heightAtIndexPath = [NSMutableDictionary dictionary];
    }
    return _heightAtIndexPath;
}


- (void)finsh
{
    [_nextButton setTitle:@"提交" forState:UIControlStateNormal];
  //   [_nextButton addTarget:self action:@selector(summit) forControlEvents:UIControlEventTouchUpInside];
}




@end
